/**
 * 表单数据自动暂存
 * 2017-08-02
 * Juliana
 */
(function (W, $, layui) {
    "use strict";
    W.fCache = function (params) {
        this.params = $.extend({
            fCacheKey: null,//暂存的key
            isCache: true,//是否开启自动暂存
            cacheCallback: function () {
            },//获取到缓存后加载的方法
            getFormData: function () {
            },//获取要保存的数据
            cacheTime: 30//多少秒缓存一次
        }, params);
    }

    fCache.prototype = {
        init: function () {
            this._loadFCache();
            this._cacheByTime();
            return this;
        },
        //删除缓存
        removeCache:function () {
            var that = this;
            util.removeStorage(that.params.fCacheKey + '_f_cache');
        },
        //开启定时缓存
        _cacheByTime: function () {
            var that = this;
            setInterval(function () {
                var formData = that.params.getFormData();
                util.setStorage(that.params.fCacheKey + '_f_cache', formData);
                // console.log('已自动保存');
                // layui.use('layer', function () {
                //     layer.msg('已自动保存', {icon: 1});
                // });
            }, this.params.cacheTime * 1000);
        },
        //加载缓存数据
        _loadFCache: function () {
            var fCacheKey = this.params.fCacheKey, callback = this.params.cacheCallback;
            if (util.getStorage(fCacheKey + '_f_cache')) {
                layui.use('layer', function () {
                    var layer = layui.layer;
                    layer.open({
                        content: '您有未保存的草稿，是否加载？'
                        , btn: ['加载', '下次提醒', '不再提醒']
                        , yes: function (index, layero) {
                            var cacheData = util.getStorage(fCacheKey + '_f_cache');
                            // util.removeStorage(fCacheKey + '_f_cache');
                            for (var name in cacheData) {
                                var $formItem = $("#myForm").find('[name="' + name + '"]');
                                if ($formItem.length > 1) {
                                    for (var i = 0; i < $formItem.length; i++) {
                                        var item = $formItem[i];
                                        if (item.type === 'radio') {
                                            if (item.value === cacheData[name]) {
                                                $(item).attr('checked', 'checked');
                                            } else {
                                                $(item).removeAttr('checked');
                                            }
                                        }
                                    }
                                } else {
                                    $formItem.val(cacheData[name]);
                                }
                                if ($formItem.data('linkUe')) {
                                    try{
                                        UE.getEditor($formItem.data('linkUe')).setContent(cacheData[name]);
                                    }catch(e){
                                        console.warn(e);
                                    };
                                }
                            }
                            typeof callback === 'function' && callback(cacheData);
                            layer.close(index);
                        }, btn2: function (index, layero) {
                            layer.close(index);
                        }, btn3: function (index, layero) {
                            util.removeStorage(fCacheKey + '_f_cache');
                            layer.close(index);
                        }
                    });
                });
            }
        }
    };
}(window, jQuery, layui));
